package com.wd.gui;

import com.wd.dominio.CentroDistribucion;
import com.wd.dominio.Lugar;
import com.wd.dominio.Proveedor;
import com.wd.dominio.Servicio;
import com.wd.gui.controlparticular.ControlGuiLugar;
import com.wd.gui.controlparticular.ControlGuiProveedor;
import java.util.Collection;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gabylis
 */
public class VentanaConsultarProveedoresCentro extends javax.swing.JFrame {

    private Vector<CentroDistribucion> vecCentros;

    private Vector<Proveedor> vecProve;

    private Vector<Lugar> vecCiudades;

    private DefaultTableModel dm;

    private ControlGuiProveedor controlGuiProveedor;

    private ControlGuiLugar controlLugar;

    /** Creates new form VentanaConsultarProveedoresCentro */
    public VentanaConsultarProveedoresCentro(Vector<CentroDistribucion> result) {
        
        initComponents();
        java.net.URL url = getClass().getResource("Iconos/icon_016.png");
        java.awt.Image imagen = getToolkit().getImage(url);
        setIconImage (imagen);

        vecCiudades = new Vector();
        vecCentros = new Vector();
        vecProve = new Vector();

        vecCentros = result;

        controlLugar = new ControlGuiLugar();
        vecCiudades = controlLugar.traerTodosLosLugares(2);

        for (CentroDistribucion centroDistribucion : result) {
            this.comboCentro.addItem(centroDistribucion.getNombre());
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        comboCentro = new javax.swing.JComboBox();
        labelCentro = new javax.swing.JLabel();
        labelIdNomb = new javax.swing.JLabel();
        labelTelefonoNomb = new javax.swing.JLabel();
        labelDireccionNomb = new javax.swing.JLabel();
        labelDirNomb = new javax.swing.JLabel();
        labelId = new javax.swing.JLabel();
        labelTelefono = new javax.swing.JLabel();
        labelDir = new javax.swing.JLabel();
        labelUbicacion = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaProveedores = new javax.swing.JTable();
        buttonCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Proveedores por Centro de Distribución");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Proveedores por Centro de Distribucion", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        comboCentro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCentroActionPerformed(evt);
            }
        });

        labelCentro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelCentro.setText("Centro de Distribución");

        labelIdNomb.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelIdNomb.setText("ID:");

        labelTelefonoNomb.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelTelefonoNomb.setText("Teléfono:");

        labelDireccionNomb.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelDireccionNomb.setText("Dirección:");

        labelDirNomb.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelDirNomb.setText("Ubicación:");

        labelId.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelId.setForeground(new java.awt.Color(0, 102, 204));
        labelId.setText("ID");

        labelTelefono.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelTelefono.setForeground(new java.awt.Color(0, 102, 204));
        labelTelefono.setText("Telefono");

        labelDir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelDir.setForeground(new java.awt.Color(0, 102, 204));
        labelDir.setText("Direccion");

        labelUbicacion.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelUbicacion.setForeground(new java.awt.Color(0, 102, 204));
        labelUbicacion.setText("ciudad+estado");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 102, 204), 1, true), "Proveedores Asociados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tablaProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tablaProveedores);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonCerrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        buttonCerrar.setText("Cerrar");
        buttonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCentro)
                    .addComponent(labelIdNomb)
                    .addComponent(labelTelefonoNomb)
                    .addComponent(labelDireccionNomb)
                    .addComponent(labelDirNomb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 162, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelUbicacion)
                    .addComponent(labelDir)
                    .addComponent(labelTelefono)
                    .addComponent(labelId)
                    .addComponent(comboCentro, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(131, 131, 131))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(233, 233, 233)
                .addComponent(buttonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(214, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCentro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCentro))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIdNomb)
                    .addComponent(labelId))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTelefonoNomb)
                    .addComponent(labelTelefono))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDireccionNomb)
                    .addComponent(labelDir))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDirNomb)
                    .addComponent(labelUbicacion))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonCerrar)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboCentroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCentroActionPerformed
        int select = -1;
        int codigoCentro = -1;
        int cityId = -1;
        String telefono = "";
        String direccion = "";
        String ubicacion = "";

        select = this.comboCentro.getSelectedIndex();
        CentroDistribucion centrico = new CentroDistribucion();
        centrico = this.vecCentros.elementAt(select);

        cityId = centrico.getLugarId();
        codigoCentro = centrico.getCodigo();
        telefono = centrico.getTelefono();
        direccion = centrico.getDireccion();
        ubicacion = this.getUbicacion(cityId);

        this.labelId.setText(codigoCentro+"");
        this.labelTelefono.setText(telefono);
        this.labelDir.setText(direccion);
        this.labelUbicacion.setText(ubicacion);

        this.llenarTabla(codigoCentro);
    }//GEN-LAST:event_comboCentroActionPerformed

    private void buttonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCerrarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_buttonCerrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<CentroDistribucion> result) {
                new VentanaConsultarProveedoresCentro(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCerrar;
    private javax.swing.JComboBox comboCentro;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCentro;
    private javax.swing.JLabel labelDir;
    private javax.swing.JLabel labelDirNomb;
    private javax.swing.JLabel labelDireccionNomb;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelIdNomb;
    private javax.swing.JLabel labelTelefono;
    private javax.swing.JLabel labelTelefonoNomb;
    private javax.swing.JLabel labelUbicacion;
    private javax.swing.JTable tablaProveedores;
    // End of variables declaration//GEN-END:variables

    public void llenarTabla(int codigoCentro){

        dm = new DefaultTableModel();
        dm.addColumn("RIF");
        dm.addColumn("Nombre");
        dm.addColumn("Telefono");
        dm.addColumn("Nombre Contacto");
        dm.addColumn("Apellido Contacto");
        dm.addColumn("Direccion");
        dm.addColumn("Ciudad");

        this.controlGuiProveedor = new ControlGuiProveedor();
        vecProve = controlGuiProveedor.traerTodosLosProveedoresCentro(codigoCentro);

        for (Proveedor prove : vecProve) {
            Vector info = new Vector();
            info.addElement(prove.getRif());
            info.addElement(prove.getNombre());
            info.addElement(prove.getTelefono());
            info.addElement(prove.getNombreContacto());
            info.addElement(prove.getApellidoContacto());
            info.addElement(prove.getDireccion());
            info.addElement(prove.getNombreCiudad());
            dm.addRow(info);
        }
        this.tablaProveedores.setModel(dm);
    }

    public String getUbicacion(int cityId){
        String nombreCity= "";
        String estado = "";
        String ubicacion = "";

        for (Lugar ciudad : vecCiudades) {
            if (ciudad.getId() == cityId){
                nombreCity = ciudad.getNombrePropio();
                estado = ciudad.getNombrePertenece();
                ubicacion = nombreCity + ", " + estado;
            }
        }
        return ubicacion;
    }
}
